package com.example.xxcjgl.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.xxcjgl.data.api.RetrofitClient
import com.example.xxcjgl.data.models.Resource
import com.example.xxcjgl.data.models.Student
import com.example.xxcjgl.data.repository.StudentRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

// StudentViewModel.kt
@HiltViewModel  // 确保有这个注解
class StudentViewModel @Inject constructor(
    private val repository: StudentRepository
) : ViewModel() {
    private val _students = MutableStateFlow<Resource<List<Student>>>(Resource.Initial)
    val students: StateFlow<Resource<List<Student>>> = _students

    private val _addScoreState = MutableStateFlow<Resource<Unit>>(Resource.Initial)
    val addScoreState: StateFlow<Resource<Unit>> = _addScoreState

    private val _currentStudent = MutableStateFlow<Student?>(null)
    val currentStudent: StateFlow<Student?> = _currentStudent

    init {
        loadStudents()
    }

    // 加载所有学生
    private fun loadStudents() {
        viewModelScope.launch {
            _students.value = Resource.Loading()
            try {
                val studentList = repository.getAllStudents()
                _students.value = Resource.Success(studentList)
            } catch (e: Exception) {
                _students.value = Resource.Error(
                    message = e.message ?: "加载学生列表失败",
                    data = null
                )
            }
        }
    }

    fun loadFirstUnencryptedStudent() {
        viewModelScope.launch {
            try {
                val response = RetrofitClient.apiService.getFirstUnencryptedStudent()
                if (response.success) {
                    _currentStudent.value = response.data
                }
            } catch (e: Exception) {
                // 处理错误
            }
        }
    }

    fun searchStudents(keyword: String) {
        viewModelScope.launch {
            _students.value = Resource.Loading()
            try {
                val response = RetrofitClient.apiService.getStudents(keyword)
                _students.value = Resource.Success(response.data ?: emptyList())
            } catch (e: Exception) {
                _students.value = Resource.Error("搜索失败")
            }
        }
    }

    fun addScore(
        studentId: String,
        mathScore: Float,
        chineseScore: Float,
        useEncryption: Boolean = false,
        encryptionKey: String? = null
    ) {
        viewModelScope.launch {
            _addScoreState.value = Resource.Loading()
            try {
                // 调用 repository 的方法来添加成绩
                // 注意：这里需要根据您的 repository 的方法来调整
                repository.addScore(
                    studentId = studentId,
                    mathScore = mathScore,
                    chineseScore = chineseScore,
                    useEncryption = useEncryption,
                    encryptionKey = encryptionKey
                )
                _addScoreState.value = Resource.Success(Unit)
            } catch (e: Exception) {
                _addScoreState.value = Resource.Error(
                    e.message ?: "添加成绩失败",
                    data = null
                )
            }
        }
    }

    fun verifyEncryptionKey(studentId: Int, key: String): Boolean {
        // 实现密钥验证逻辑
        return true
    }

    fun resetAddScoreState() {
        _addScoreState.value = Resource.Initial
    }
}